//
//  ViewController.m
//  GPUImagePicture2
//
//  Created by Xwoder on 2017/1/17.
//  Copyright © 2017年 Xwoder. All rights reserved.
//
#import "ViewController.h"
#import "GPUImage.h"

@interface ViewController ()

@property (nonatomic, strong) GPUImagePicture *picture;

@property (nonatomic, strong) GPUImageTiltShiftFilter *tiltShiftFilter;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    CGRect viewBounds = self.view.bounds;
    
    // 原始图片
    UIImage *image = [UIImage imageNamed:@"input.jpeg"];
    GPUImagePicture *picture = [[GPUImagePicture alloc] initWithImage:image];
    
    // 移轴滤镜
    GPUImageTiltShiftFilter *tiltShiftFilter = [[GPUImageTiltShiftFilter alloc] init];
    tiltShiftFilter.blurRadiusInPixels = 0;
    tiltShiftFilter.frameProcessingCompletionBlock = ^void(GPUImageOutput *filter, CMTime time) {
        NSLog(@"%s", __FUNCTION__);
    };
    // 强制滤镜按照指定的尺寸输出图片，忽略宽高比
    [tiltShiftFilter forceProcessingAtSize:viewBounds.size];
    [picture addTarget:tiltShiftFilter];
    
    GPUImageView *view = [[GPUImageView alloc] initWithFrame:viewBounds];
    [view setBackgroundColorRed:1 green:1 blue:1 alpha:1];
    [tiltShiftFilter addTarget:view];
    [self.view addSubview:view];
    
    // 渲染图片
    [picture processImage];
    
    self.picture = picture;
    self.tiltShiftFilter = tiltShiftFilter;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.tiltShiftFilter.blurRadiusInPixels <= 10) {
        self.tiltShiftFilter.blurRadiusInPixels +=2;
    }
    [self.picture processImage];
}

@end
